package com.mg.axechen.livedata

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer

class MainActivity : AppCompatActivity() {

    var liveData1: MutableLiveData<Int> = MutableLiveData()
    var liveData2: MutableLiveData<String> = MutableLiveData()

    var mediatorLiveData: MediatorLiveData<Any> = MediatorLiveData()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mediatorLiveData.addSource(liveData1,dataObserver2)
        mediatorLiveData.addSource(liveData2,dataObserver)
        mediatorLiveData.observe(this, Observer {
            Log.i("liveData5",it.toString())
        })
        liveData1.postValue(1000)
        liveData2.postValue("axeChen")


        mediatorLiveData.postValue("xxxx")
    }
    private var dataObserver2 = object :Observer<Int>{
        override fun onChanged(t: Int?) {
            Log.i("liveData4",t.toString())
        }
    }
    private var dataObserver =object :Observer<Any> {
        override fun onChanged(t: Any?) {
            Log.i("liveData3",t.toString())
        }
    }

}